package com.kk.service.impl;

import com.kk.model.User;
import com.kk.repository.UserRrepository;
import com.kk.service.UserService;
import org.hibernate.annotations.common.util.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author: kevin
 * @date: 2018-10-14 16:55
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRrepository userRrepository;
    @Override
    public List<User> selectUsers(String name) {
        List<User> userList;
        if (StringHelper.isNotEmpty(name)) {
            userList = userRrepository.findByNameLike(name);
        }else{
            userList = userRrepository.findAll();
        }
        return userList;
    }

    @Override
    public boolean saveUser(User user) {
        User result = userRrepository.save(user);
        if (result!=null) {
            return true;
        }
        return false;
    }

    @Override
    public User findUserById(String id) {
        Optional<User> user = userRrepository.findById(id);
        if (user.isPresent()) {
            return user.get();
        }
        return null;
    }
}
